﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Word = Microsoft.Office.Interop.Word;

namespace PMSSys
{
    public partial class PatientView : Form
    {
        private bool isModified = false;

        public PatientView(string p_id, patientController pccontroller)
        {
            InitializeComponent();
            
            ppprocessor = pccontroller;
            if (ppprocessor.DbConnect.Permissionlevel == "Secretary")
            {
                icdButton.Enabled = false;
                treatmentsButton.Enabled = false;
                followupsButton.Enabled = false;
                bMTButton.Enabled = false;
                exportButton.Enabled = false;
            }
            pObject = new Patient();
            if (systemAdding = String.IsNullOrEmpty(p_id))
            {
                exportButton.Enabled = false;
                bday = false;
                PBDate.Format = DateTimePickerFormat.Custom;
                PBDate.CustomFormat = " ";
            }
            else
            {
                this.p_id = int.Parse(p_id);
                pObject.PatientID = int.Parse(p_id);
                ppprocessor.objectFiller(pObject);
                this.Text = "Patient - " + pObject.FName + " " + pObject.LName;
                PLName.Text = pObject.LName;
                PMname.Text = pObject.MName;
                PFName.Text = pObject.FName;
                PBDate.Value = pObject.Birthday;
                if (pObject.Sex == "M")
                    radioButton1.Checked = true;
                else
                    radioButton2.Checked = true;
                PAddress.Text = pObject.Address;
                PTelNo.Text = pObject.TelNo;
                PCelNo.Text = pObject.CelNo;
                PFatherName.Text = pObject.FatherName;
                PFOccupation.Text = pObject.FatherOccu;
                PMotherName.Text = pObject.MotherName;
                PMOccupation.Text = pObject.MotherOccu;
                PSiblingsCount.Text = pObject.SiblingCount.ToString();
                PAnnualIncome.Text = pObject.AnnualInc.ToString();
                PReferrer.Text = pObject.Refferal;
                isModified = false;
            }


        }
        private Boolean systemAdding, bday;
        private patientController ppprocessor;
        private Patient pObject;
        private int p_id;


        private void icdButton_Click(object sender, EventArgs e)
        {
            if (systemAdding)
            {
                DialogResult omg = uiUtility.confirmSaveDialog();
                if (omg != DialogResult.Cancel)
                {
                    if (omg == DialogResult.Yes)
                    {
                        if (addPatientGuiChecker())
                        {
                           addSave();
                           InitialClinicalDataView newWindow = new InitialClinicalDataView(pObject, ppprocessor);
                           newWindow.ShowDialog();                               
                        }
                    }
                }
            }
            else
            {
                ///update ng patient
                InitialClinicalDataView newWindow = new InitialClinicalDataView(pObject, ppprocessor, true);
                newWindow.ShowDialog();
            }
        }

        private void treatmentsButton_Click(object sender, EventArgs e)
        {
            if (systemAdding)
            {
                DialogResult res = uiUtility.confirmSaveDialog();

                if (res == DialogResult.Yes)
                {
                    if (addPatientGuiChecker())
                    {
                        addSave();
                        TreatmentListViewer newWindow = new TreatmentListViewer(ppprocessor, pObject);
                        newWindow.ShowDialog();
                    }
                }
            }
            else
            {
                TreatmentListViewer newWindow = new TreatmentListViewer(ppprocessor, pObject);
                newWindow.ShowDialog();
            }
        }

        private void followupsButton_Click(object sender, EventArgs e)
        {
            if (systemAdding)
            {
                DialogResult res = uiUtility.confirmSaveDialog();

                if (res == DialogResult.Yes)
                {
                    if (addPatientGuiChecker())
                    {
                        addSave();
                        FollowUpView newWindow = new FollowUpView(ppprocessor, pObject);
                        newWindow.ShowDialog();
                    }
                }
            }
            else
            {
                FollowUpView newWindow = new FollowUpView(ppprocessor, pObject);
                newWindow.ShowDialog();
            }
        }

        private void bMTButton_Click(object sender, EventArgs e)
        {
            if (systemAdding)
            {
                DialogResult res = uiUtility.confirmSaveDialog();

                if (res == DialogResult.Yes)
                {
                    if (addPatientGuiChecker())
                    {
                        addSave();
                        BoneMarrowList newWindow = new BoneMarrowList(ppprocessor, pObject);
                        newWindow.ShowDialog();
                    }
                }
            }
            else
            {
                BoneMarrowList newWindow = new BoneMarrowList(ppprocessor, pObject);
                newWindow.ShowDialog();
            }
        }

        private void paymentsButton_Click(object sender, EventArgs e)
        {
            if (systemAdding)
            {
                DialogResult res = uiUtility.confirmSaveDialog();

                if (res == DialogResult.Yes)
                {
                    if (addPatientGuiChecker())
                    {
                        addSave();
                        PaymentListView newWindow = new PaymentListView(ppprocessor, pObject);
                        newWindow.ShowDialog();
                    }
                }
            }
            else
            {
                PaymentListView newWindow = new PaymentListView(ppprocessor, pObject);
                newWindow.ShowDialog();
            }
        }
        private void exportButton_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.Filter = "Microsoft Word Document|*.docx";
            saveFileDialog1.Title = "Save Exported Record to";

            exportController exp = new exportController();
            try
            {

                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    String FetchFileName = saveFileDialog1.FileName;
                    if (exp.export(this.p_id, ppprocessor, FetchFileName))
                    {
                        MessageBox.Show("Export of Files successfully done", "PMS Export System", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);

                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace);
            }
        }

        private void anyvalue_changed(object sender, EventArgs e)
        {
            isModified = true;
        }

        private void PatientView_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (isModified)
            {
                DialogResult res = uiUtility.confirmSaveDialog();
                if (res != DialogResult.Cancel)
                {
                    if (res == DialogResult.Yes)
                    {
                        if (!addPatientGuiChecker())
                        {
                            e.Cancel = true;
                        }
                        else
                        {
                            if (systemAdding)
                            {
                                ppprocessor.add(addAttributes(this.pObject));
                                addICD();
                            }
                            else
                            {
                                ppprocessor.update(addAttributes(this.pObject));
                            }
                        }
                    }

                }
                else
                    e.Cancel = true;

            }
        }
        private bool addPatientGuiChecker()
        {
            DateTime dateNow = DateTime.Now;
            int i;
            int bDateRes = DateTime.Compare(DateTime.Parse(PBDate.Value.ToString()), dateNow);
            if (bDateRes >= 0 || bday == false)
            {
                DialogResult validDate = uiUtility.validDate("Invalid Birthday");
                return false;
            }
            if (radioButton1.Checked == false && radioButton2.Checked == false)
            {
                MessageBox.Show("Select a gender");
                return false;
            }
            if (PFName.Text == string.Empty || PLName.Text == string.Empty)
            {
                MessageBox.Show("Please enter patient name.", "No name found", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            if (!int.TryParse(PSiblingsCount.Text, out i))
            {
                MessageBox.Show("Please enter a numeric value for sibling count.", "Numeric Value", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            if (!int.TryParse(PAnnualIncome.Text, out i))
            {
                MessageBox.Show("Please enter a numeric value for annual income.", "Numeric Value", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            return true;
        }
        private Patient addAttributes(Patient pObject)
        {
            int i = 0;
            pObject.PatientID = this.p_id;
            pObject.LName = PLName.Text;
            pObject.FName = PFName.Text;
            pObject.MName = PMname.Text;
            pObject.Birthday = DateTime.Parse(PBDate.Value.ToString());
            if (radioButton1.Checked)
            {
                pObject.Sex = "M";
            }
            else if (radioButton2.Checked)
            {
                pObject.Sex = "F";
            }
            pObject.Address = PAddress.Text;
            pObject.TelNo = PTelNo.Text;
            pObject.CelNo = PCelNo.Text;
            pObject.FatherName = PFatherName.Text;
            pObject.FatherOccu = PFOccupation.Text;
            pObject.MotherName = PMotherName.Text;
            pObject.MotherOccu = PMOccupation.Text;
            int.TryParse(PSiblingsCount.Text, out i);
            pObject.SiblingCount = i;
            i = 0;
            int.TryParse(PAnnualIncome.Text, out i);
            pObject.AnnualInc = i;
            pObject.Refferal = PReferrer.Text;

            return pObject;
        }
        private void closeLogic(object sender, EventArgs e)
        {
            this.Close();
        }

        private void PatientView_Load(object sender, EventArgs e)
        {
            ToolTip t1 = uiUtility.tooltips();
            t1.SetToolTip(this.icdButton, "Initial Clinical Data");
            t1.SetToolTip(this.treatmentsButton, "Treatment & Medication Plan");
            t1.SetToolTip(this.followupsButton, "Follow-Up");
            t1.SetToolTip(this.bMTButton, "Bone Marrow Tests");
            t1.SetToolTip(this.paymentsButton, "Payments");
            t1.SetToolTip(this.exportButton, "Export Record");
            t1.SetToolTip(this.button6, "Back");
        }
        private void addSave()
        {
            ppprocessor.add(addAttributes(this.pObject));
            this.pObject.PatientID = int.Parse(ppprocessor.DbConnect.lastID());   
            ppprocessor.objectFiller(pObject);
            this.Text = "Patient - " + pObject.FName + " " + pObject.LName;
            addICD();
            exportButton.Enabled = true && ppprocessor.DbConnect.Permissionlevel == "Physician";
            isModified = false;
            systemAdding = false;
        }
        private void addICD()
        {
            icdController icdCtrl = new icdController();
            icdCtrl.DbConnect = ppprocessor.DbConnect;
            icdCtrl.addTemp(int.Parse(ppprocessor.DbConnect.lastID()));
        }
        private void PBDate_ValueChanged_1(object sender, EventArgs e)
        {
            isModified = true;
            bday = true;
            PBDate.Format = DateTimePickerFormat.Long;
        }

        private void splitContainer1_Panel2_Paint(object sender, PaintEventArgs e)
        {

        }
    }
}
